/*
   Copyright (C) 2006
   Andrew Madigan

This file is part of nhttpd.

nhttpd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

nhttpd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with nhttpd; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

#include <fcntl.h>
#include <ltdl.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include <nhttpd/log.h>
#include <nhttpd/detect.h>
#include "nhconfig.h"
#include "nhexec.h"

#ifdef NHTTP_THREADS
void onSigInt(int signum)	{
	NH_log(LOG_NOTICE, "Stopping Server...");
	NH_stopServer();
}
#endif

void squelchSig(int signum)	{
	//NH_log(LOG_DEBUG, "%d signal received", signum);
}

int main(int argc, char* argv[])	{
	NH_init();
	/*
	For the morbidly curious, all this does is mask SIGPIPE in a reliable way
	whether we are using threads or not, I personally think this is absolutely
	ridiculous just to get over the already bad idea of sending SIGPIPE just
	because the client closed the socket.
	*/
	sigset_t* maskset = malloc(sizeof(sigset_t));
	sigemptyset(maskset);
	struct sigaction* masker = malloc(sizeof(struct sigaction));
	masker->sa_handler = squelchSig;
	masker->sa_mask = *maskset;
	masker->sa_flags = 0;
	sigaction(SIGPIPE, masker, NULL);
	free(masker);
	
	printf("%s version %s compiled %s at %s\n", PACKAGE_NAME, PACKAGE_VERSION,
				__DATE__, __TIME__);

#ifdef NH_CONFIG_DEFCONF
	char* confpath = NH_CONFIG_DEFCONF
#elif defined(NH_DEFCONF)
	char* confpath = NH_DEFCONF;
#else
	char* confpath = "nhttpd.conf";
#endif
	bool daemonMode = false;
	int opt = getopt(argc, argv, "c:hdv");
	while (opt != -1)	{
		switch (opt)	{
			case 'v' :
				return 0;
			case 'd' :
				daemonMode = true;
				break;
			case 'c':
				confpath = strdup(optarg);
				break;
			case 'h' :
				printf("Usage: %s -c file -h -v -d\n"
						, argv[0]);
				printf("\t-c file\tUse file and configuration (default is %s)\n",
							confpath);
				printf("\t-h\tPrint this help\n");
				printf("\t-v\tPrint version and exit\n");
				printf("\t-d\tLog as a daemon\n");
				return 0;
			case '?':
				return -1;
		}
		opt = getopt(argc, argv, "c:hdv");
	}
#ifdef NHTTP_THREADS
	/*
	We don't do this without threads because the handler could get caught doing
	something and not return for a while. The action handler for this only tells
	the server to stop, which at this point only stops the connection loop.
	Later on, we might do this per-thread, using setjmp.
	*/
	signal(SIGINT, onSigInt);
#endif
	
	NH_openlog(!daemonMode, PACKAGE, LOG_CONS, LOG_USER);
  
  int conFd = open(confpath, O_RDONLY, 0);
  if (conFd < 0) {
	  NH_log(LOG_CRIT, "Unable to open configuration file %s\n", confpath);
	  return -1;
  }
  
  Configuration* config = NH_readConfig(conFd);
  
  close(conFd);
  
  if (!config) {
	  NH_log(LOG_CRIT, "Fatal Error reading configuration %s\n", confpath);
  }
  
  NH_Process* mainProc = NP_Create(config);
  
  NP_Run(mainProc);
  
  O_Free(config);
  O_Free(mainProc);
  NH_exit();
  return 0;
}
